//
//  PhotoBrowserAnimator.swift
//  新浪微博
//
//  Created by 焦林生 on 15/11/19.
//  Copyright © 2015年 焦林生. All rights reserved.
//

import UIKit

//MARK: 照片浏览器展现转场代理
protocol PhotoBrowserPresentDelegate: NSObjectProtocol {

    /**
    参与转场动画的图像视图
    
    - parameter indexPath: 照片索引
    
    - returns: 参与转场动画的全新图像视图
    */
    func imageViewForPresent(indexPath: NSIndexPath) -> UIImageView
    
    /**
    展现转场图像起始位置
    
    - parameter indexPath: 照片索引
    
    - returns: 相对于屏幕的位置
    */
    func photoBrowserPresentFromRect(indexPath: NSIndexPath) -> CGRect
    
    /**
    展现动画结束时的屏幕位置
    
    - parameter indexPath: 照片索引
    
    - returns: 相对于屏幕的位置
    */
    func photoBrowserPresentToRect(indexPath: NSIndexPath) -> CGRect
    
}

//MARK:- 照片浏览器解除转场代理
protocol PhotoBrowserDismissDelegate: NSObjectProtocol {

    //解除转场动画的图像视图
    func imageViewForDismiss() -> UIImageView
    //解除转场动画的图像索引
    func indexPathForDismiss() -> NSIndexPath
}

//转场动画代理对象
class PhotoBrowserAnimator: NSObject, UIViewControllerTransitioningDelegate {

    //是否展现标记
    private var isPresent = false
    
    //展现转场代理对象
    weak var presentDelegate: PhotoBrowserPresentDelegate?
    //解除转场代理对象
    weak var dismissDelegate: PhotoBrowserDismissDelegate?
    
    //展现动画对应的照片索引
    var indexPath: NSIndexPath?
    
    /**
    设置代理参数
    
    - parameter presentDelegate: 展现转场代理
    - parameter dismissDelegate: 解除转场代理
    - parameter indexPath:       动画对应的索引
    */
    func setDelegateParams(presentDelegate: PhotoBrowserPresentDelegate, dismissDelegate: PhotoBrowserDismissDelegate,indexPath: NSIndexPath) {
    
        self.presentDelegate = presentDelegate
        self.dismissDelegate = dismissDelegate
        self.indexPath = indexPath
    }
    
    //返回'提供展现动画'的对象
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        
        isPresent = true
        return self
    }
    
    //返回'提供解除转场动画'的对象
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        
        isPresent = false
        return self
    }
}

// MARK: - UIViewControllerAnimatedTransitioning
//提供具体的转场动画实现
extension PhotoBrowserAnimator: UIViewControllerAnimatedTransitioning
{
    /**
    动画时常
    
    - parameter transitionContext: 转场上下文
    
    - returns: 动画时长
    */
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        return 0.5
    }
    
    /**
    转场动画实现方法 一旦实现此函数 系统的动画方法  将由程序员负责
    
    - parameter transitionContext: 转场上下文  提供转场动画的所有细节
    */
    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        
        isPresent ? presentAnimation(transitionContext) : dismissAnimation(transitionContext)
    }
    /**
    展现转场动画
    
    - parameter transitionContext: 转场上下文
    */
    private func presentAnimation(transitionContext: UIViewControllerContextTransitioning) {
        
        guard let presentDelegate = presentDelegate, indexPath = indexPath else {
        
            return
        }
        //1 真正要展现的视图
        //1>获取 modal 要展现的根视图
        let toView = transitionContext.viewForKey(UITransitionContextToViewKey)!
        
        //2> 添加到容器视图
        transitionContext.containerView()?.addSubview(toView)
        
        
        //2 获取目标控制器 照片查看控制器
        let toVC = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey) as! PhotoBrowserViewController
        
        toVC.collectionView.hidden = true
        
        
        //2 转场动画中的'大米'视图
        //1> 建立转场动画图像视图'
            let iv = presentDelegate.imageViewForPresent(indexPath)
        //2> 设置起始位置
        iv.frame = presentDelegate.photoBrowserPresentFromRect(indexPath)
        //3> 将视图添加到容器视图
        transitionContext.containerView()?.addSubview(iv)
        //2> 设置透明度
        toView.alpha = 0.0
        
        //3 动画转场
        UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
            
            iv.frame = presentDelegate.photoBrowserPresentToRect(indexPath)
            
            toView.alpha = 1.0
            
            }) { (_) -> Void in
                
                //删除动画转场的图像视图'
                iv.removeFromSuperview()
                toVC.collectionView.hidden = false
                
                transitionContext.completeTransition(true)
        }
    }
    
    /**
    解除转场动画
    
    - parameter transitionContext: 转场上下文
    */
    private func dismissAnimation(transitionContext: UIViewControllerContextTransitioning) {
        
        guard let presentDelegate = presentDelegate,dismissDelegate = dismissDelegate else {
        
            return
        }
    
        let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)!
        
        //2 转场动画中的大米视图
        let iv = dismissDelegate.imageViewForDismiss()
        let indexPath = dismissDelegate.indexPathForDismiss()
        transitionContext.containerView()?.addSubview(iv)
        
         UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
            
            fromView.alpha = 0.0
            iv.frame = presentDelegate.photoBrowserPresentFromRect(indexPath)
            
            }) { (_) -> Void in
                
                //删除转场动画的图像的视图
                iv.removeFromSuperview()
                
                fromView.removeFromSuperview()
                transitionContext.completeTransition(true)
        }
    }
    
}






